<?php
class I18n {
    
    var $mLang = "en";
    var $mDefaultLang = "en";
    var $cDateFormat = "d.m.Y";
    var $cDateTimeFormat = "d.m.Y H:i:s";
    var $cFloatDelimiter = ",";
    var $cCharSet = "";
    var $cTimeString = "";
    var $cMonthNames;
    var $cWeekDayNames;
    var $cWeekDayShortNames;
    var $cStartWeek;
    var $cHolidays;
    
    var $mKeys = array();
    var $mSourceDir = "";
    
    function I18n() {
    	$this->mKeys["test"] = array("one"=>"word");
    }
    
    function setSourceDir($dir) {
    	$this->mSourceDir = $dir;
    }
    
    function &getKeys() {
    	return $this->mKeys;
    }
    
    function getKey($kword) {
    	$value = "";
    	$pos = strpos($kword, ".");
		if (!($pos === false)) {
           $p = explode(".", $kword);
           $str = "";
           foreach ($p as $w) {
               $str .= "['".$w."']";
           }
           eval("\$value = @\$this->mKeys".$str."['__v'];");
		} else {
           @$value = $this->mKeys[$kword];
		}
    	return $value;
    }
    
    function getDateFormat() {
    	return $this->cDateFormat;
    }
    
    function getDateTimeFormat() {
    	return $this->cDateTimeFormat;
    }

    function getFloatDelimiter() {
    	return $this->cFloatDelimiter;
    }
    
    function getCharSet() {
    	return $this->cCharSet;
    }
    
    function getTimeString() {
    	return $this->cTimeString;
    }
    
    function getMonthName($id) {
    	$id = intval($id);
    	return $this->cMonthNames[$id];
    }
    
    function getMonthes() {
    	return $this->cMonthNames;
    }
    
    function getWeekDayShortNames() {
    	return $this->cWeekDayShortNames;
    }
    
    function getWeekDayName($id) {
    	$id = intval($id);
    	return $this->cWeekDayNames[$id];
    }
    
    function getLang() {
    	return $this->mLang;
    }
    
    function load($localName) {
    	
    	$this->mLang = $localName;
    	
		$kw = array();
		$filename = strtolower($localName) . '.lang.php';
		$inc_filename = $this->mSourceDir . '/' . $this->mDefaultLang . '.lang.php';

		if (file_exists($inc_filename)) {
			include_once($inc_filename);
			
			$this->cCharSet = $i18n_charset;
			$this->cTimeString = $i18n_timestring;
			$this->cMonthNames = $i18n_month;
    		$this->cWeekDayNames = $i18n_week;
    		$this->cWeekDayShortNames = $i18n_week_sn;
    		$this->cStartWeek = $i18n_start_week;
    		$this->cDateFormat = $i18n_format_date;
			$this->cDateTimeFormat = $i18n_format_date . " " . $i18n_format_time;
			$this->cFloatDelimiter = $i18n_float_delimiter;
			if (defined("SHOW_HOLIDAYS") && SHOW_HOLIDAYS == "true") {
				$this->cHolidays = @$i18n_holidays;
			}
			foreach ($i18n_key_words as $kword=>$value) {
			   $pos = strpos($kword, ".");
			   if (!($pos === false)) {
                   $p = explode(".", $kword);
                   $str = "";
                   foreach ($p as $w) {
                       $str .= "[\"".$w."\"]";
                   }
                   $value = str_replace("'","&rsquo;",$value);
                   eval("\$this->mKeys".$str."['__v']='".$value."';");
               } else {
                   $this->mKeys[$kword] = $value;
               }
			}
			$this->mKeys["float_delimiter"] = $i18n_float_delimiter;
		}
		
		$inc_filename = $this->mSourceDir . '/' . $filename;
		if (file_exists($inc_filename) && $localName!=$this->mDefaultLang) {
			require_once($inc_filename);
			
			$this->cCharSet = $i18n_charset;
			$this->cTimeString = $i18n_timestring;
			$this->cMonthNames = $i18n_month;
    		$this->cWeekDayNames = $i18n_week;
    		$this->cWeekDayShortNames = $i18n_week_sn;
    		$this->cStartWeek = $i18n_start_week;
    		$this->cDateFormat = $i18n_format_date;
			$this->cDateTimeFormat = $i18n_format_date . " " . $i18n_format_time;
			$this->cFloatDelimiter = $i18n_float_delimiter;
			if (defined("SHOW_HOLIDAYS") && SHOW_HOLIDAYS == "true") {
				$this->cHolidays = @$i18n_holidays;
			}
			foreach ($i18n_key_words as $kword=>$value) {
			   if (!$value) continue;
			   $pos = strpos($kword, ".");
			   if (!($pos === false)) {
                   $p = explode(".", $kword);
                   $str = "";
                   foreach ($p as $w) {
                       $str .= "[\"".$w."\"]";
                   }
                   $value = str_replace("'","&rsquo;",$value);
                   eval("\$this->mKeys".$str."['__v']='".$value."';");
               } else {
                   $this->mKeys[$kword] = $value;
               }
			}
			$this->mKeys["float_delimiter"] = $i18n_float_delimiter;
    		return true;
		}

	}
}
?>